﻿package org.yoao.crm.tools;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TimeTool {
	/**
	 * @author yxwonline
	 * @param time
	 * @param format
	 * @return
	 */
	public static String getTime(String time, String format){
		if(time != null){
			try{
				SimpleDateFormat formatter;
			    int tempPos = time.indexOf("AD") ;
			    time = time.trim() ;
			    formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss z");
			    if(tempPos>-1){
			      time=time.substring(0,tempPos)+
			           "公元"+time.substring(tempPos+"AD".length());
			      formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss z");
			    }
			    
			    tempPos=time.indexOf("-");
			    if(tempPos>-1&&(time.indexOf(" ")<0)&&time.length()<11){
			    	return time;
			    }
			    
			    if(time.indexOf("/")>-1){
			    	if(time.substring(0, time.indexOf("/")).length()<4){
			    		formatter = new SimpleDateFormat("MM/dd/yyyy");
			    	}
			    }
			    else if(tempPos>-1&&(time.indexOf(" ")<0)){
			      formatter = new SimpleDateFormat ("yyyyMMddHHmmssZ");
			    }
			    else if(tempPos<0&&(time.indexOf(" ")<0)&&(time.indexOf("/")<0)&&time.length()<15){
				      formatter = new SimpleDateFormat ("yyyyMMdd");
				}
			    else if((time.indexOf("/")>-1) &&(time.indexOf(" ")>-1)){
			      formatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");
			    }
			    else if((time.indexOf("/")>-1) &&(time.indexOf(" ")<0)&&time.length()<11){
				      return time.replaceAll("/", "-");
				}
			    else if((time.indexOf("/")>-1) &&(time.indexOf(" ")<0)){
				      formatter = new SimpleDateFormat ("yyyy/MM/dd");
				}
			    else if((time.indexOf("-")>-1) &&(time.indexOf(" ")>-1)){
			      formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
			    }
			    else if((time.indexOf("-")>-1) &&(time.indexOf(" ")<0)){
				      formatter = new SimpleDateFormat ("yyyy-MM-dd");
				}
			    else if((time.indexOf("/")>-1) &&(time.indexOf("am")>-1) ||(time.indexOf("pm")>-1)){
			    	  formatter = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a");
			    }
			    else if((time.indexOf("-")>-1) &&(time.indexOf("am")>-1) ||(time.indexOf("pm")>-1)){
			      formatter = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a");
			    }
			    ParsePosition pos = new ParsePosition(0);
			    java.util.Date ctime = formatter.parse(time, pos);
			    
			    SimpleDateFormat sdf = new SimpleDateFormat (format);
			    return sdf.format(ctime);
			}catch(Exception e){
				e.printStackTrace();
				System.out.println("ERROR TIME String is :"+time);
				return time;
			}
		}else
			return time;
	}
	
	public static String getSystemTime(){
		try{
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
		}catch(Exception e){
			return null;
		}
	}
	
	public static String getSystemDate(){
		try{
			return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
		}catch(Exception e){
			return null;
		}
	}
}
